package com.cxrj.zhsq.autonomy.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.autonomy.dao.AutonomyCommitteeMapper;
import com.cxrj.zhsq.autonomy.dao.AutonomyCommitteeOrderMapper;
import com.cxrj.zhsq.autonomy.dao.AutonomyForwardMapper;
import com.cxrj.zhsq.autonomy.dao.AutonomyGroupMapper;
import com.cxrj.zhsq.autonomy.dao.AutonomySecretaryMapper;
import com.cxrj.zhsq.autonomy.entity.AutonomyCommittee;
import com.cxrj.zhsq.autonomy.entity.AutonomyCommitteeOrderWithBLOBs;
import com.cxrj.zhsq.autonomy.entity.AutonomyForward;
import com.cxrj.zhsq.autonomy.entity.AutonomyGroup;
import com.cxrj.zhsq.autonomy.entity.AutonomySecretary;
import com.cxrj.zhsq.autonomy.service.AutonomyService;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.utils.HtmlUtil;
import com.cxrj.zhsq.utils.IpUtil;

@Service
public class AutonomyServiceImpl implements AutonomyService {

	@Resource
	private AutonomySecretaryMapper autonomySecretaryMapper;

	@Resource
	private AutonomyCommitteeMapper autonomyCommitteeMapper;

	@Resource
	private AutonomyCommitteeOrderMapper autonomyCommitteeOrderMapper;

	@Resource
	private AutonomyForwardMapper autonomyForwardMapper;

	@Resource
	private AutonomyGroupMapper autonomyGroupMapper;

	@Resource
	private AppUserMapper appUserMapper;

	@Override
	public Map<String, Object> selectSecretaryPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("start", Integer.parseInt(start));
		// sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", request.getParameter("name"));
		sqlMap.put("tyep", request.getParameter("tyep"));

		// 获取数据库总记录数
		recordsTotal = autonomySecretaryMapper.getSecretaryCount(sqlMap) + "";

		List<Map<String, Object>> list = autonomySecretaryMapper.selectSecretaryPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectSecretaryById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AutonomySecretary autonomySecretary = autonomySecretaryMapper.selectByPrimaryKey(id);
		if (autonomySecretary.getFeedbackContent() != null && !autonomySecretary.getFeedbackContent().equals("")) {
			autonomySecretary.setFeedbackContent(HtmlUtil.getAbsSource(autonomySecretary.getFeedbackContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		if (autonomySecretary.getAuditReason() != null && !autonomySecretary.getAuditReason().equals("")) {
			autonomySecretary.setAuditReason(HtmlUtil.getAbsSource(autonomySecretary.getAuditReason(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		resultMap.put("secretary", autonomySecretary);
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		return resultMap;
	}

	@Override
	public Map<String, Object> updateSecretary(AutonomySecretary autonomySecretary) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = autonomySecretaryMapper.updateByPrimaryKeySelective(autonomySecretary);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertSecretary(String userId, String appContent, String pics) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		AutonomySecretary autonomySecretary = new AutonomySecretary();
		autonomySecretary.setId(UUID.randomUUID().toString());
		autonomySecretary.setAppAccount(appUser.getUserAccount());
		autonomySecretary.setAppContent(appContent);
		autonomySecretary.setAppDate(new Date());
		autonomySecretary.setAppName(appUser.getUserName());
		autonomySecretary.setAppNo(appUser.getUserIdCard());
		if (!pics.equals("")) {
			autonomySecretary.setAppPicture(pics.substring(1));
		}
		autonomySecretary.setAppTel(appUser.getUserMobile());
		autonomySecretary.setCreateTime(new Date());
		autonomySecretary.setStatus("0");
		Integer c = autonomySecretaryMapper.insertSelective(autonomySecretary);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectSecretary(String status, String userAccount, Integer dataIndex, Integer pageCount) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("userAccount", userAccount);
		sqlMap.put("status", status);
		sqlMap.put("start", dataIndex);
		sqlMap.put("length", pageCount);
		List<AutonomySecretary> list = autonomySecretaryMapper.selectSecretary(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommitteePager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("start", Integer.parseInt(start));
		// sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", request.getParameter("nmae"));

		// 获取数据库总记录数
		recordsTotal = autonomyCommitteeMapper.getCommitteeCount(sqlMap) + "";

		List<Map<String, Object>> list = autonomyCommitteeMapper.selectCommitteePager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommitteeById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AutonomyCommittee autonomyCommittee = autonomyCommitteeMapper.selectByPrimaryKey(id);
		if (autonomyCommittee.getProcess() != null && !autonomyCommittee.getProcess().equals("")) {
			autonomyCommittee.setProcess(HtmlUtil.getAbsSource(autonomyCommittee.getProcess(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		resultMap.put("committee", autonomyCommittee);
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		return resultMap;
	}

	@Override
	public Map<String, Object> updateCommittee(AutonomyCommittee autonomyCommittee) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = autonomyCommitteeMapper.updateByPrimaryKeySelective(autonomyCommittee);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommitteeAll(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("list", autonomyCommitteeMapper.selectByAll());
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		return resultMap;
	}

	@Override
	public Map<String, Object> insertCommitteeOrder(String userId, String committeeId, String orderTime, HttpServletRequest request) throws ParseException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		AutonomyCommittee autonomyCommittee = autonomyCommitteeMapper.selectByPrimaryKey(committeeId);
		AutonomyCommitteeOrderWithBLOBs autonomyCommitteeOrder = new AutonomyCommitteeOrderWithBLOBs();
		autonomyCommitteeOrder.setId(UUID.randomUUID().toString());
		autonomyCommitteeOrder.setAppAccount(appUser.getUserAccount());
		autonomyCommitteeOrder.setAppDate(new Date());
		autonomyCommitteeOrder.setAppName(appUser.getUserName());
		autonomyCommitteeOrder.setAppNo(appUser.getUserIdCard());
		autonomyCommitteeOrder.setAppTel(appUser.getUserMobile());
		autonomyCommitteeOrder.setCreateTime(new Date());
		autonomyCommitteeOrder.setProject(autonomyCommittee.getProject());
		autonomyCommitteeOrder.setCommitteeId(committeeId);
		autonomyCommitteeOrder.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(orderTime));
		autonomyCommitteeOrder.setS1("0");
		autonomyCommitteeOrder.setS2((int) ((Math.random() * 9 + 1) * 100000) + "");
		Integer c = autonomyCommitteeOrderMapper.insertSelective(autonomyCommitteeOrder);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommitteeOrder(String status, String userAccount, Integer dataIndex, Integer pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("userAccount", userAccount);
		sqlMap.put("status", status);
		sqlMap.put("start", dataIndex);
		sqlMap.put("length", pageCount);
		List<AutonomyCommitteeOrderWithBLOBs> list = autonomyCommitteeOrderMapper.selectCommitteeOrder(sqlMap);
		for (AutonomyCommitteeOrderWithBLOBs bloBs : list) {
			if (bloBs.getFeedbackContent() != null && !bloBs.getFeedbackContent().equals("")) {
				bloBs.setFeedbackContent(HtmlUtil.getAbsSource(bloBs.getFeedbackContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
			}
			if (bloBs.getAuditReason() != null && !bloBs.getAuditReason().equals("")) {
				bloBs.setAuditReason(HtmlUtil.getAbsSource(bloBs.getAuditReason(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
			}
		}
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommitteeOrderById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AutonomyCommitteeOrderWithBLOBs bloBs = autonomyCommitteeOrderMapper.selectByPrimaryKey(id);
		if (bloBs.getFeedbackContent() != null && !bloBs.getFeedbackContent().equals("")) {
			bloBs.setFeedbackContent(HtmlUtil.getAbsSource(bloBs.getFeedbackContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		if (bloBs.getAuditReason() != null && !bloBs.getAuditReason().equals("")) {
			bloBs.setAuditReason(HtmlUtil.getAbsSource(bloBs.getAuditReason(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		resultMap.put("order", bloBs);
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAndUpdateCommittee(AutonomyCommittee autonomyCommittee) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (!autonomyCommittee.getId().equals("")) {
			c = autonomyCommitteeMapper.updateByPrimaryKeySelective(autonomyCommittee);
		} else {
			autonomyCommittee.setId(UUID.randomUUID().toString());
			autonomyCommittee.setS1("1");
			autonomyCommittee.setCreateTime(new Date());
			c = autonomyCommitteeMapper.insertSelective(autonomyCommittee);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectCommitteeOrderPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("start", Integer.parseInt(start));
		// sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", request.getParameter("name"));
		sqlMap.put("type", request.getParameter("type"));

		// 获取数据库总记录数
		recordsTotal = autonomyCommitteeOrderMapper.getCommitteeOrderCount(sqlMap) + "";

		List<Map<String, Object>> list = autonomyCommitteeOrderMapper.selectCommitteeOrderPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateCommitteeOrder(AutonomyCommitteeOrderWithBLOBs autonomyCommitteeOrder) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = autonomyCommitteeOrderMapper.updateByPrimaryKeySelective(autonomyCommitteeOrder);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectForwardPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("start", Integer.parseInt(start));
		// sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", request.getParameter("name"));
		sqlMap.put("type", request.getParameter("type"));

		// 获取数据库总记录数
		recordsTotal = autonomyForwardMapper.getForwardCount(sqlMap) + "";

		List<Map<String, Object>> list = autonomyForwardMapper.selectForwardPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectForwardById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AutonomyForward autonomyForward = autonomyForwardMapper.selectByPrimaryKey(id);
		resultMap.put("forward", autonomyForward);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateForward(AutonomyForward autonomyForward) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = autonomyForwardMapper.updateByPrimaryKeySelective(autonomyForward);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertForward(String userId, String appContent, String pics) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		AutonomyForward autonomyForward = new AutonomyForward();
		autonomyForward.setId(UUID.randomUUID().toString());
		autonomyForward.setAppAccount(appUser.getUserAccount());
		autonomyForward.setAppContent(appContent);
		autonomyForward.setAppDate(new Date());
		autonomyForward.setAppName(appUser.getUserName());
		autonomyForward.setAppNo(appUser.getUserIdCard());
		if (!pics.equals("")) {
			autonomyForward.setAppPicture(pics.substring(1));
		}
		autonomyForward.setAppTel(appUser.getUserMobile());
		autonomyForward.setCreateTime(new Date());
		autonomyForward.setStatus("0");
		Integer c = autonomyForwardMapper.insertSelective(autonomyForward);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectGroupPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";

		sqlMap.put("start", Integer.parseInt(start));
		// sqlMap.put("condition", " where" + str);
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", request.getParameter("name"));

		// 获取数据库总记录数
		recordsTotal = autonomyGroupMapper.getGroupCount(sqlMap) + "";

		List<Map<String, Object>> list = autonomyGroupMapper.selectGroupPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectGroupById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AutonomyGroup autonomyGroup = autonomyGroupMapper.selectByPrimaryKey(id);
		resultMap.put("group", autonomyGroup);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAndUpdateGroup(AutonomyGroup autonomyGroup) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (!autonomyGroup.getId().equals("")) {
			c = autonomyGroupMapper.updateByPrimaryKeySelective(autonomyGroup);
		} else {
			autonomyGroup.setId(UUID.randomUUID().toString());
			autonomyGroup.setS1("1");
			autonomyGroup.setCreateTime(new Date());
			c = autonomyGroupMapper.insertSelective(autonomyGroup);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;

	}

	@Override
	public Map<String, Object> selectGroup(String name) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("name", name);
		resultMap.put("list", autonomyGroupMapper.selectGroup(sqlMap));
		return resultMap;
	}
}
